# coding=utf-8
from script import data

__author__ = 'awh'

import web

render=web.template.render('templates/')

urls=(
    "","show",
    "/","show",
    "/(.*)","showcontent",
)

def AllToUTF(text):
    """
    convert string coding to utf-8
    """
    for c in ('jp','utf8', 'gbk', 'big5', 'euc_kr','utf16','utf32'):
        try:
            text= text.decode(c).encode('utf8')
            break
        except:
            continue
    return text


class show(object):
    def GET(self):
        #print 'Content-Type: text/plain'
        novels= data.db.GqlQuery('SELECT *  FROM novel')
        return render.listnovel(novels)
        #webinput=web.input()
        #i=webinput['id']
        #print i

    def POST(self):
        x = web.input(myfile={})
        a= x['myfile'].filename.decode('utf-8')
        b= x['myfile'].value
        c= x['myfile'].file.read()
        n= data.novel(name=a,cont=b)
        n.put()
        raise web.seeother('/'+a)

class showcontent(object):
    def GET(self,name):
        web.header('Content-type','text/html','charset=utf8')
        #return render.index(name)
        novels= data.db.GqlQuery('SELECT *  FROM novel where name=:name1 limit 1',name1=name)
        for novel in novels:
            for each in novel.cont.split('\n'):
                print each+'<br>'


app=web.application(urls,locals())